using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DynamicTextTest
{
    public partial class CodeForm : Form
    {
        private string _Code;
        private System.CodeDom.Compiler.CodeDomProvider _CodeProvider;
        private SharpMud.Text.Dynamic.Processing.IProcessedDynamicTextBody _pDTB;
        public CodeForm(string code, System.CodeDom.Compiler.CodeDomProvider codeProvider, SharpMud.Text.Dynamic.Processing.IProcessedDynamicTextBody pDTB)
        {
            _Code = code;
            _CodeProvider = codeProvider;
            _pDTB = pDTB;

            InitializeComponent();

            this.textBox1.Text = _Code;
            this.textBox1.ReadOnly = true;
        }

        private void btnCompileAndEvaluate_Click(object sender, EventArgs e)
        {
            SharpMud.Text.Dynamic.Compilation.BuildManager bobTheBuilder = new SharpMud.Text.Dynamic.Compilation.BuildManager(new System.CodeDom.Compiler.CompilerParameters(), this._CodeProvider);
            bobTheBuilder.Compile(_pDTB);
            SharpMud.Text.Dynamic.ICompiledDynamicTextBody cDTB = bobTheBuilder.GetHandle(_pDTB);

            (new EvaluationForm(cDTB)).ShowDialog();
        }
    }
    internal class SimpleEvalContext : SharpMud.Text.Dynamic.IDynamicTextExecutionContext
    {
        private System.IO.TextWriter _OutputWriter;
        public SimpleEvalContext(System.IO.TextWriter outputWriter)
        {
            _OutputWriter = outputWriter;
        }

        #region IDynamicTextExecutionContext Members

        public System.IO.TextWriter Output
        {
            get { return _OutputWriter; }
        }

        public object this[string parameterName]
        {
            get
            {
                if (parameterName.Trim().ToLower() == "output")
                    return _OutputWriter;
                else
                    return null;
            }
        }

        #endregion
    }
}